package web_controller

import (
	"github.com/duke-git/lancet/v2/convertor"
	"github.com/gin-gonic/gin"
	"strconv"
	"transport/framework/fw_db/fw_db_self"
	"transport/framework/fw_middleware"
	"transport/framework/fw_utils"
	"transport/web/web_mapper"
	"transport/web/web_model"
)

type driverController struct {
}

func (controller *driverController) ItemById(c *gin.Context) {
	id, _ := convertor.ToInt(c.Param("id"))
	mapper := web_mapper.DriverMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	item, err := mapper.QueryById(id)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	fw_utils.OK(c, item, "ok")
}
func (controller *driverController) DelById(c *gin.Context) {
	id, _ := convertor.ToInt(c.Param("id"))
	mapper := web_mapper.DriverMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	err := mapper.DeleteById(id)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	fw_utils.OK(c, "", "ok")
}
func (controller *driverController) DelByIds(c *gin.Context) {
	var ids []int64
	err := c.ShouldBindJSON(&ids)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	mapper := web_mapper.DriverMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	err = mapper.DeleteByIds(ids)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	fw_utils.OK(c, "", "ok")
}
func (controller *driverController) CreateOrUpdateItem(c *gin.Context) {
	var data web_model.TransDriver
	err := c.ShouldBindJSON(&data)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	mapper := web_mapper.DriverMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	_, err = mapper.CreateOrUpdate(&data)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	fw_utils.OK(c, "", "ok")
}
func (controller *driverController) CreateItem(c *gin.Context) {
	var data web_model.TransDriver
	err := c.ShouldBindJSON(&data)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	mapper := web_mapper.DriverMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	ret, err := mapper.Create(&data)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	fw_utils.OK(c, ret, "ok")
}
func (controller *driverController) UpdateItem(c *gin.Context) {
	var data web_model.TransDriver
	err := c.ShouldBindJSON(&data)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	mapper := web_mapper.DriverMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	ret, err := mapper.Update(&data)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	fw_utils.OK(c, ret, "ok")
}
func (controller *driverController) Page(c *gin.Context) {
	var err error
	var from, limit = 1, 10
	if tmp := c.Request.FormValue("pageSize"); tmp != "" {
		limit, _ = strconv.Atoi(tmp)
	}
	if tmp := c.Request.FormValue("pageIndex"); tmp != "" {
		from, _ = strconv.Atoi(tmp)
	}
	name := c.Request.FormValue("name")
	mapper := web_mapper.DriverMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	items, total, err := mapper.Page(from, limit, name)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	fw_utils.PageOK(c, items, total, from, limit, "ok")
}
func (controller *driverController) List(c *gin.Context) {
	var err error
	mapper := web_mapper.DriverMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	items, total, err := mapper.List()
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	fw_utils.ListOK(c, items, total, "ok")
}

func DriverRoute(r *gin.Engine) {
	controller := driverController{}
	v1 := r.Group("/trans").Use(fw_middleware.JwtAuthMiddleware())
	{
		v1.GET("/v1/driver/page", controller.Page)
		v1.GET("/v1/driver/list", controller.List)
		v1.GET("/v1/driver/:id", controller.ItemById)
		v1.DELETE("/v1/driver/:id", controller.DelById)
		v1.DELETE("/v1/driver/batchByIds", controller.DelByIds)
		v1.PUT("/v1/driver", controller.UpdateItem)
		v1.POST("/v1/driver", controller.CreateItem)
		v1.POST("/v1/driverItemCreateOrUpdate", controller.CreateOrUpdateItem)
	}
}
